package com.fastloan.mgt.application.contract.service.impl;

import com.fastloan.common.model.CheckResult;
import com.fastloan.mgt.application.base.enums.ApplicationStatusEnum;
import com.fastloan.mgt.application.base.model.Application;
import com.fastloan.mgt.application.base.model.ApplicationVo;
import com.fastloan.mgt.application.base.service.ApplicationService;
import com.fastloan.mgt.application.contract.model.ApplicationContract;
import com.fastloan.mgt.application.contract.model.CustomerContractInfo;
import com.fastloan.mgt.application.contract.service.CustomerContractService;
import com.fastloan.mgt.application.repay.dao.ZyApplicationRepaymentPlanMapper;
import com.fastloan.mgt.application.repay.model.ZyApplicationRepaymentPlan;
import com.fastloan.mgt.model.model.ApplicationModel;
import com.fastloan.mgt.model.service.ModelCalculateService;
import com.fastloan.mgt.processcontrol.base.service.ProcessDispatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author yanwe
 * createTime 2017-12-2017/12/20 17:21
 */
@Service
public class CustomerContractServiceImpl implements CustomerContractService {

    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private ApplicationContractServiceImpl applicationContractService;

    @Autowired
    private ModelCalculateService modelCalculateService;

    @Autowired
    private ZyApplicationRepaymentPlanMapper zyApplicationRepaymentPlanMapper;

    @Override
    public CustomerContractInfo getCustomerContractInfo(String applicationId) {
        Application application = applicationService.selectByPrimaryKey(applicationId);
        if (application == null){
            return null;
        }
        ApplicationVo applicationVo = applicationContractService.getApplicationVo(applicationId);
        ApplicationContract contractInfo = applicationContractService.getContractInfo(applicationId);
        ApplicationModel lastModelResult = modelCalculateService.getLastModelResult(applicationId);
        CustomerContractInfo customerContractInfo = new CustomerContractInfo();
        customerContractInfo.setApplicationId(applicationId);
        customerContractInfo.setApplyResult(lastModelResult.getPass());
        customerContractInfo.setApplyTime(applicationVo.getApplyTime());
        customerContractInfo.setLoanAmount(applicationVo.getApplyAmount());
        customerContractInfo.setAuditAmount(contractInfo.getLoanAmount());
        customerContractInfo.setLoanRate(contractInfo.getLoanMonthRate());
        customerContractInfo.setRepayment(applicationVo.getRepayment());
        ZyApplicationRepaymentPlan zyApplicationRepaymentPlan = new ZyApplicationRepaymentPlan();
        zyApplicationRepaymentPlan.setAppId(applicationId);
        List<ZyApplicationRepaymentPlan> planList = zyApplicationRepaymentPlanMapper.select(zyApplicationRepaymentPlan);
        if (planList.isEmpty()){
            return customerContractInfo;
        }
        planList.sort(Comparator.comparing(ZyApplicationRepaymentPlan::getRepaymentOverdueDate));
        ZyApplicationRepaymentPlan plan = planList.get(0);
        BigDecimal monthMoney = plan.getPlanPeriodInterest()
                .add(plan.getPlanPeriodPrincipal()).add(plan.getCustomerPenalty());
        customerContractInfo.setLoanStartTime(new Date());
        customerContractInfo.setLoanEndTime(planList.get(planList.size()-1).getRepaymentOverdueDate());
        customerContractInfo.setRepaymentMonthDate(plan.getRepaymentOverdueDate());
        customerContractInfo.setRepaymentMonthMoney(monthMoney);
        customerContractInfo.setLoanContract("");
        return customerContractInfo;
    }

    @Override
    public CheckResult agreeContract(String applicationId, boolean agree){
        Application application = applicationService.selectByPrimaryKey(applicationId);
        boolean haveNext = ProcessDispatcher.isHaveNext(application.getProcessId());
        if (haveNext) {
            return CheckResult.fail("该进件流程未结束无法同意或拒绝签约！");
        }
        applicationService.modifyApplicationStatus(applicationId, agree ? ApplicationStatusEnum.IS_SIGNED
                : ApplicationStatusEnum.CUSTOMER_REJECT_SIGNED);
        return CheckResult.success();
    }
}
